if (!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.user.Master")) {
    /**
     *
     */
    jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.userMgt.user", function (machines) {
    });

    /**
     *
     */
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.userMgt.user.Master",
            jsx3.gui.LayoutGrid,
            null,
            function (master) {

                master.prototype.initialize = function() {
                    this.subscribeAll()
                    if (jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.window.Window")) {
                        // set the window instance
                        this.window = this.getAncestorOfType(tibco.admin.enterpriseAssets.userMgt.window.Window)
                        if (this.window) {
                            this.setWindowId(this.window.getId());
                        }
                    }
                    // set the matrix
                    this.mtx = this.getDescendantOfName("mtxMaster_24_B");
                    this.curSearchQuery = "*";
                    var me = this;
                    this.mtx.publishSelect = function(strRECORDIDS) {
                        me.publishSelect(strRECORDIDS);
                    };
                    this.realmEditable = "false";

                    this.searchBtn = this.getDescendantOfName("btnSearchGo_24_B");
                    this.searchBtn.doSearch = function() {
                        me.curSearchQuery = me.getDescendantOfName("txtSearchKey_24_B").getValue();
                        me.callsearchUsers();
                    }
                    setTimeout(function(){me.callsearchUsers()} ,200)
                }

                master.prototype.subscribeAll = function() {
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.userMgt.EventID.ADD_ITEM_SUCCESS, this, this.selectAdded);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.userMgt.EventID.ADD_ITEM_CANCELED, this, this.refreshSelected);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_REMOVED, this, this.refreshMaster);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.userMgt.EventID.USER_DISABLED_REFRESH, this, this.setRefreshDisabled);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.userMgt.EventID.USER_ENABLED_REFRESH, this, this.setRefreshEnable);
                }

                master.prototype.unsubscribeAll = function() {
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.userMgt.EventID.ADD_ITEM_SUCCESS, this);
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.userMgt.EventID.ADD_ITEM_CANCELED, this);
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_REMOVED, this);
                }

                master.prototype.setWindowId = function(strId) {
                    this.windowId = strId
                }

                master.prototype.getWindowId = function() {
                    return this.windowId
                }

                master.prototype.setSelectedRecord = function(strRECORDID) {
                    this.selected = strRECORDID
                }

                master.prototype.getSelectedRecord = function() {
                    return this.selected
                }



                //call this method to begin the service call (eg.service.callremoveUsers();)
                master.prototype.callremoveUsers = function() {
                    tibco.admin.Main.getServer().getCache().setDocument("XMLCacheMaster_24_B", this.mtx.getXML());
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/rules/removeUsers.xml", "removeUsers", "Remove Users");
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onremoveUsersSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onremoveUsersError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onremoveUsersInvalid);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getUserMgtEndpointURL());
                    objService.doCall();
                };

                master.prototype.onremoveUsersSuccess = function(objEvent) {
                    this.setSelectedRecord(null)
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_REMOVED
                    msg.windowId = this.getWindowId();
                    msg.master = "tibco.admin.enterpriseAssets.userMgt.user.Master"
                    tibco.admin.PubSub.publish(msg);
                    tibco.admin.util.Log.USERMGT.trace("Success, The service call was successful.");
                };

                master.prototype.onremoveUsersError = function(objEvent) {
                    var myStatus = objEvent.target.getRequest().getStatus();
                    tibco.admin.util.Log.USERMGT.error("Error, The service call failed. The HTTP Status code is: " + myStatus);
                };

                master.prototype.onremoveUsersInvalid = function(objEvent) {
                    tibco.admin.util.Log.USERMGT.error("Invalid, The following message node just failed validation:\n\n" + objEvent.message);
                };

                master.prototype.doRefresh = function() {
                    this.callsearchUsers();
                }

                master.prototype.refreshMaster = function(objEvent) {
                    if (!objEvent.isRetained && objEvent.master == "tibco.admin.enterpriseAssets.userMgt.user.Master") {
                        this.callsearchUsers()
                    }
                }

                //call this method to begin the service call (eg.service.callsearchUsers();)
                master.prototype.callsearchUsers = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/rules/searchUsers.xml", "searchUsers", "Search Users");
                    //subscribe and call
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onsearchUsersSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onsearchUsersError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onsearchUsersInvalid);
                    objService.searchQuery = this.curSearchQuery;
                    objService.searchLimit = 500;
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getUserMgtEndpointURL());
                    objService.doCall();
                };

                master.prototype.onsearchUsersSuccess = function(objEvent) {
                    try {
                        // Use xslt to map the input to a cdf
                        var sourceXML = objEvent.target.getInboundDocument();
                        var transformXSL = new jsx3.xml.Document().load(tibco.admin.Main.resolveURI("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/xsl/searchUser.xsl"));
                        var xsltprocessor = new jsx3.xml.Processor(sourceXML, transformXSL)
                        var transformedDocument = xsltprocessor.transformToObject();
                        this.mtx.setSourceXML(transformedDocument)
                        this.state = (objEvent.target.isRealmWriteable == "true")? jsx3.gui.Form.STATEENABLED : jsx3.gui.Form.STATEDISABLED;
                        this.realmEditable = objEvent.target.isRealmWriteable;
                        if (this.getSelectedRecord()) {
                            // select the detalis for this selected
                            if (this.mtx.getRecord(this.getSelectedRecord())) {
                                var array = new Array;
                                array[0] = this.getSelectedRecord();
                                this.publishSelect(array);
                                this.mtx.selectRecord(this.getSelectedRecord());
                            }
                            else {
                                this.setSelectedRecord(null)
                                this.mtx.deselectAllRecords()
                                var msg = new Object();
                                msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_MULTISELECT;
                                msg.windowId = this.getWindowId();
                                msg.master = "tibco.admin.enterpriseAssets.userMgt.user.Master"
                                tibco.admin.PubSub.publish(msg);
                                this.setListButtonsEnabled();
                            }
                        }
                        else {
                            this.publishSelect(this.mtx.getSelectedIds());
                        }
                        this.mtx.repaint();
                        tibco.admin.util.Log.USERMGT.trace("Repainted useres list");
                    }
                    catch(ex) {
                        tibco.admin.util.Log.USERMGT.error(ex);
                        tibco.admin.Main.logException(ex);
                    }
                };

                master.prototype.onsearchUsersError = function(objEvent) {
                    var myStatus = objEvent.target.getRequest().getStatus();
                    tibco.admin.util.Log.USERMGT.error("Error, The service call failed. The HTTP Status code is: " + myStatus);
                };

                master.prototype.onsearchUsersInvalid = function(objEvent) {
                    tibco.admin.util.Log.USERMGT.error("Invalid, The following message node just failed validation:\n\n" + objEvent.message);
                };

                /**
                 * Sets whether the controls are enabled. Disabled controls do not respond to user interaction.
                 */
                master.prototype.setListButtonsEnabled = function() {
                    var buttonPane = this.getDescendantOfName("blkBtnBars_24_B_1").getDescendantsOfType(jsx3.gui.Button, false);
                    //var buttonPane = tibco.admin.Main.getServer().getJSXByName("blkBtnBars_24_B_1").getDescendantsOfType(jsx3.gui.Button, false);
                    for (var i = 0; i < buttonPane.length; i++) {
                        //if(buttonPane[i].getName() != "btnSuperUsers_24_B") { //the superuser button should be enabled in all realms
                        buttonPane[i].setEnabled(this.state, true);
                        // }
                    }
                    if ((this.state == jsx3.gui.Form.STATEENABLED) && this.mtx.getSelectedNodes().size() == 0) {
                        this.getDescendantOfName("btnDel_24_B").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    }
                    if ((this.state == jsx3.gui.Form.STATEENABLED) && this.mtx.getSelectedNodes().size() == 0) {
                        this.getDescendantOfName("btnDel_24_B").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    }

                }

                /**
                 * called when a user in user list is selected. publishes an event object (javascript object) with the
                 * following named properties: subject(tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_SELECTED):
                 * windowId(id of the parent window instance); userId(userId); isWriteable(boolean) ;
                 * isGetGroupsFprUserEfficient(boolean)
                 *
                 *@param strRecordId {String} ?the jsxid attribute of the data record to return.(selected item)
                 */
                master.prototype.publishSelect = function(strRECORDIDS) {
                    try {
                        var strRowID = strRECORDIDS[0];
                        this.setSelectedRecord(strRowID)
                        this.setListButtonsEnabled();
                        if (strRECORDIDS.length != 1 ) {
                            var msg = new Object();
                            msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_MULTISELECT;
                            msg.windowId = this.getWindowId();
                            msg.master = "tibco.admin.enterpriseAssets.userMgt.user.Master"
                            tibco.admin.PubSub.publish(msg);
                            return;
                        }
                        var record = this.mtx.getXML().selectSingleNode("//record[@jsxid ='" + strRowID + "']");
                        if (record) {
                            var msg = new Object();
                            msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_SELECTED
                            msg.windowId = this.getWindowId();
                            msg.userId = record.getAttribute("jsxid");
                            msg.isWriteable = (record.getAttribute("isWriteable") == 'true')?true:false;
                            msg.isGetGroupsForUserEfficient = (record.getAttribute("isGetGroupsForUserEfficient") == 'true')?true:false;
                            msg.isRealmWriteable = this.realmEditable;
                            msg.strPath = "tibco/admin/enterpriseAssets/userMgt/window/user/ui/detail.xml";
                            tibco.admin.PubSub.publish(msg);
                        }
                    }
                    catch(ex) {
                        tibco.admin.util.Log.USERMGT.error(ex);
                        tibco.admin.Main.logException(ex);
                    }
                }

                master.prototype.showSetPswPolicy = function() {
                    var loadTo = this.window;
                    var body = tibco.admin.Main.getServer().getBodyBlock();
                    var parent = this.window.getParent();
                    while (parent) {
                        if (parent == body) {
                            loadTo = body;
                            break;
                        }
                        parent = parent.getParent();
                    }
                    tibco.admin.Main.loadUIResourceAsync(loadTo, "tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/ui/setPswPolicy.xml");
                }

                master.prototype.addNewUser = function(strPath) {
                    this.mtx.deselectAllRecords()
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.ADD_ITEM_REQUIRED;
                    msg.strPath = strPath;
                    msg.windowId = this.getWindowId();
                    tibco.admin.PubSub.publish(msg);

                    msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.USER_DISABLED_REFRESH;
                    tibco.admin.PubSub.publish(msg);
                }

                master.prototype.selectAdded = function(objEvent) {
                    if (objEvent.windowId == this.getWindowId() && !objEvent.isRetained) {
                        this.setSelectedRecord(objEvent.username)
                        this.callsearchUsers()
                    }
                }

                master.prototype.refreshSelected = function() {
                    if (this.getSelectedRecord())
                        this.mtx.selectRecord(this.getSelectedRecord())
                }

                master.prototype.loadPasswordPolicyDialog = function() {
                    var loadTo = this.window;
                    var body = tibco.admin.Main.getServer().getBodyBlock();
                    var parent = this.window.getParent();
                    while (parent) {
                        if (parent == body) {
                            loadTo = body;
                            break;
                        }
                        parent = parent.getParent();
                    }
                    tibco.admin.Main.loadUIResource(loadTo, "tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/ui/setPswPolicy.xml");
                }

                master.prototype.loadSuperUserDialog = function() {
                    var loadTo = this.window;
                    var body = tibco.admin.Main.getServer().getBodyBlock();
                    var parent = this.window.getParent();
                    while (parent) {
                        if (parent == body) {
                            loadTo = body;
                            break;
                        }
                        parent = parent.getParent();
                    }
                    var me = this;
                    tibco.admin.Main.loadUIResourceAsync(loadTo, "tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/ui/superUsers.xml", me, me.superUserLoaded, true);
                };

                master.prototype.superUserLoaded = function(msg) {
                    this.superUserDlg = msg.obj;
                    this.superUserDlg.setWindow(this.window);
                }

                master.prototype.setRefreshDisabled = function(objEvent) {
                    this.getDescendantOfName("btnRefresh_24_B").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                };

                master.prototype.setRefreshEnable = function(objEvent) {
                    this.getDescendantOfName("btnRefresh_24_B").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                }

            });
}
